package com.lonely.web.controller.goods;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lonely.common.core.domain.api.R;
import com.lonely.goods.domain.dto.goods.*;
import com.lonely.goods.domain.vo.goods.*;
import com.lonely.goods.service.IGoodsNotesService;
import com.lonely.goods.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 黄志标
 * @date: 2025/6/25 15:32
 * @description: 商品请求处理器
 */
@RestController
@RequestMapping("/goodsManager/goods")
@Api(tags = "商品管理/商品")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IGoodsNotesService goodsNotesService;

    /**
     * 分页查询
     *
     * @param queryDto
     * @return
     */
    @PostMapping("/page")
    @ApiOperation(value = "打版列表分页查询")
//    @SaCheckPermission("goods:patternMaking:page")
    public R<Page<QueryGoodsPageVO>> page(@RequestBody @Validated QueryGoodsPageDTO queryDto) {
        return R.data(this.goodsService.page(queryDto));
    }

    /**
     * 查询通过率
     *
     * @param queryDto
     * @return
     */
    @PostMapping("/queryPassRate")
    public R<QueryPassRateVO> queryPassRate(@RequestBody @Validated QueryPassRateDTO queryDto) {
        return R.data(this.goodsService.queryPassRate(queryDto));
    }

    /**
     * 审核批量打版
     *
     * @param reviewDTO
     * @return
     */
    @PostMapping("/review")
    @SaCheckPermission("goods:patternMaking:review")
    public R<Boolean> review(@RequestBody @Validated GoodsReviewDTO reviewDTO) {
        this.goodsService.review(reviewDTO);
        return R.status(true);
    }


    /**
     * 添加新商品
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加新商品")
    @SaCheckPermission("goods:patternMaking:addGoods")
    public R<Boolean> addGoods() {
        this.goodsService.addGoods();
        return R.status(true);
    }

    /**
     * 根据商品id获取商品款信息
     *
     * @param goodsId
     * @return
     */
    @GetMapping("/designInfo/{goodsId}")
    @ApiOperation(value = "根据商品id获取商品款信息")
    public R<GoodsDesignInfoVO> getGoodsDesignInfo(@PathVariable("goodsId") Long goodsId) {
        return R.data(this.goodsService.getGoodsDesignInfo(goodsId));
    }


    /**
     * 修改商品款信息
     *
     * @param submitDTO
     * @return
     */
    @PostMapping("/submitDesignInfo")
    @ApiOperation(value = "修改商品款信息")
    public R<Boolean> submitDesignInfo(@RequestBody @Validated GoodsDesignInfoSubmitDTO submitDTO) {
        this.goodsService.submitDesignInfo(submitDTO);
        return R.status(true);
    }


    /**
     * 添加商品备注
     *
     * @param addGoodsNotesDTO
     * @return
     */
    @PostMapping("/addNotes")
    @ApiOperation(value = "添加商品备注")
    public R<Boolean> addNotes(@RequestBody @Validated AddGoodsNotesDTO addGoodsNotesDTO) {
        this.goodsNotesService.addNotes(addGoodsNotesDTO);
        return R.status(true);
    }


    /**
     * 根据商品id获取商品报价单信息
     *
     * @param goodsId
     * @return
     */
    @GetMapping("/quotationSheet/{goodsId}")
    @ApiOperation(value = "根据商品id获取商品报价单信息")
    public R<GoodsQuotationSheetVO> getGoodsQuotationSheet(@PathVariable("goodsId") Long goodsId) {
        return R.data(this.goodsService.getGoodsQuotationSheet(goodsId));
    }

    /**
     * 修改商品报价单
     *
     * @param submitDTO
     * @return
     */
    @PostMapping("/submitQuotationSheet")
    @ApiOperation(value = "修改商品报价单")
    public R<Boolean> submitQuotationSheet(@RequestBody @Validated GoodsQuotationSheetSubmitDTO submitDTO) {
        this.goodsService.submitQuotationSheet(submitDTO);
        return R.status(true);
    }

    /**
     * 复制已有bom的数据查询
     *
     * @param queryDto
     * @return
     */
    @PostMapping("/copyBomDataQuery")
    @ApiOperation(value = "复制已有bom的数据查询")
    public R<List<CopyBomQueryVO>> copyBomDataQuery(@RequestBody @Validated CopyBomQueryDTO queryDto) {
        return R.data(this.goodsService.copyBomDataQuery(queryDto));
    }

    /**
     * 查询指定设计编码是否是套装
     *
     * @param designCode
     * @return
     */
    @GetMapping("/isSuit/{designCode}")
    @ApiOperation(value = "查询指定设计编码是否是套装")
    public R<Boolean> isSuit(@PathVariable("designCode") String designCode) {
        return R.data(this.goodsService.isSuit(designCode));
    }

    /**
     * 修改状态
     *
     * @param changeStatusDTO
     * @return
     */
    @PostMapping("/changeStatus")
    @ApiOperation(value = "修改状态")
    @SaCheckPermission("goods:patternMaking:changeStatus")
    public R<Boolean> changeStatus(@RequestBody @Validated ChangeStatusDTO changeStatusDTO) {
        this.goodsService.changeStatus(changeStatusDTO);
        return R.status(true);
    }

    /**
     * 创建订单
     *
     * @param createOrderDTO
     * @return
     */
    @PostMapping("/createOrder")
    @ApiOperation(value = "创建订单")
    public R<Boolean> createOrder(@RequestBody @Validated GoodsCreateOrderDTO createOrderDTO) {
        this.goodsService.createOrder(createOrderDTO);
        return R.status(true);
    }

    /**
     * 基于商品id查询创建订单前的商品数据
     *
     * @param goodsId
     * @return
     */
    @GetMapping("/queryGoodsByCreateOrder/{goodsId}")
    @ApiOperation(value = "基于商品id查询创建订单前的商品数据")
    public R<QueryGoodsByCreateOrderVO> queryGoodsByCreateOrder(@PathVariable("goodsId") Long goodsId) {
        return R.data(this.goodsService.queryGoodsByCreateOrder(goodsId));
    }


    /**
     * 查询指定商品指定颜色下的总成本
     *
     * @param queryDto
     * @return
     */
    @PostMapping("/queryGoodsTotalCostByColor")
    @ApiOperation(value = "查询指定商品指定颜色下的总成本")
    public R<String> queryGoodsTotalCostByColor(@RequestBody @Validated QueryGoodsTotalCostByColorDTO queryDto) {
        return R.data(this.goodsService.queryGoodsTotalCostByColor(queryDto));
    }


    /**
     * 根据sku获取对应的商品id
     *
     * @param sku
     * @return
     */
    @GetMapping("/getGoodsIdBySku/{sku}")
    @ApiOperation(value = "根据sku获取对应的商品id")
    public R<Long> getGoodsIdBySku(@PathVariable("sku") String sku) {
        return R.data(this.goodsService.getGoodsIdBySku(sku));
    }

    /**
     * 更新商品库存信息
     *
     * @param updateStorageDTO
     * @return
     */
    @PostMapping("/updateStorage")
    @ApiOperation(value = "更新商品库存信息")
    public R<Boolean> updateStorage(@RequestBody @Validated UpdateGoodStorageDTO updateStorageDTO) {
        this.goodsService.updateStorage(updateStorageDTO);
        return R.status(true);
    }

    /**
     * 同步打板信息到mes
     *
     * @return
     */
    @GetMapping("/synchronizedStyle/{goodsId}")
    public R<Boolean> synchronizedStyle(@PathVariable("goodsId") Long goodsId) {
        this.goodsService.synchronizedStyle(goodsId);
        return R.status(true);
    }


}
